package datas
{
	import main.ClientConstants;
	import main.DataSubBase;
	import main.XMLLoaderBase;
	
	import message.CommenSortRequest;
	import message.CommentBackResult;
	import message.InsertComment;
	import message.RequestComment;
	
	import struct.Scenic;
	import struct.TourElementBase;
	
	import xmlS.EatingResturantXMLLoader;
	import xmlS.HotelXMLLoader;
	import xmlS.ScenicShopXMLLoader;
	import xmlS.ScenicXmlLoader;
	
	public class ViewSpotData extends DataSubBase
	{
		private var _viewSpotsData:ScenicXmlLoader;
		private var _eatData:EatingResturantXMLLoader;
		private var _shopsData:ScenicShopXMLLoader;
		private var _hotelData:HotelXMLLoader;
		
		private var _type:int;
		private var _itemID:uint;
		private var _page:uint;
		private var _currentCommentList:Array = [];
		public function ViewSpotData()
		{
		
		}
		
		private function insertCommentData(dataList:Array,instertedList:Array):void
		{
			if(dataList == null)
			{
				_currentCommentList = [];
				return;
			}
			var scenic:TourElementBase;
			for(var i:int=0;i<instertedList.length;i++)
			{
				scenic = instertedList[i];
				if(scenic.id == _itemID)
				{
					scenic.commentList[_page] = dataList;
					_currentCommentList = dataList;
					scenic.commentPeopleNum = dataList.length;
					break;
				}
			}
		}
		
		private function checkCommentData(insertedList:Array):Boolean
		{
		   var item:TourElementBase;
		   for(var i:int=0;i<insertedList.length;i++)
		   {
			   item = insertedList[i];
			   if(item.id == _itemID && item.commentList[_page])
			   {
				   _currentCommentList = item.commentList[_page];
				   return true;
			   }
		   }
		   return false;
		}
		
		override  protected function registeredListener():void
		{
			this.registerMessageHandler(CommentBackResult.CMD,commitCallBack);
		}
		
		private function commitCallBack(value:CommentBackResult):void
		{
             if(value.requestType == ClientConstants.DETAIL_EAT + 5)
			 {
				 insertCommentData(value.records,_eatData.resturantList);
			 }else if(value.requestType == ClientConstants.DETAIL_SHOP + 5)
			 {
				 insertCommentData(value.records,_shopsData.shopList);
			 }else if(value.requestType == ClientConstants.DETAIL_VIEWSPOT + 5)
			 {
				 insertCommentData(value.records,_viewSpotsData.spotList);
			 }else if(value.requestType == ClientConstants.DETAIL_HOUSE + 5)
			 {
				 insertCommentData(value.records,_hotelData.hotelList);
			 }
 		}
		
		public function get commentListData():Array
		{
			return _currentCommentList;
		}
		
		public function loadViewSpotData(callBack:Function):void
		{
			_type = ClientConstants.DETAIL_VIEWSPOT;
			if(!_viewSpotsData)
			{
				_viewSpotsData = this.xmlLoader.scenicXml;
				_viewSpotsData.initThis(callBack);
			}else
			{
				callBack();
			}
		}
		
		public function loadHotelData(callBack:Function):void
		{
			_type = ClientConstants.DETAIL_HOUSE;
			if(!_hotelData)
			{
				_hotelData = this.xmlLoader.hotelXml;
				_hotelData.initThis(callBack);
			}else
			{
				callBack();
			}
		}
		
		public function loadEatData(callBack:Function):void
		{
			_type = ClientConstants.DETAIL_EAT;
			if(!_eatData)
			{
				_eatData = this.xmlLoader.eatingResturantXml;
				_eatData.initThis(callBack);
			}else
			{
				callBack();
			}
		}
		
		public function get shopList():Array
		{
			return _shopsData.shopList;
		}
		
		public function loadShopData(callBack:Function):void
		{
			_type = ClientConstants.DETAIL_SHOP;
			if(!_shopsData)
			{
				_shopsData = this.xmlLoader.scenicShopXml;
				_shopsData.initThis(callBack);
			}else
			{
				callBack();
			}
		}
		
		public function get viewSpotsData():ScenicXmlLoader
		{
			return _viewSpotsData;
		}
		
		public function get resturantList():Array
		{
			return _eatData.resturantList;
		}
		
		public function get hotelList():Array
		{
			return _hotelData.hotelList;
		}
		
		public function requestCommitData(itemId:uint,page:uint):void
		{
			_itemID = itemId;
			_page = page - 1;
			_currentCommentList = [];
			if(_type == ClientConstants.DETAIL_EAT)
			{
				if(checkCommentData(_eatData.resturantList)) return;
			}else if(_type == ClientConstants.DETAIL_SHOP)
			{
				if(checkCommentData(_shopsData.shopList)) return;
			}else if(_type == ClientConstants.DETAIL_VIEWSPOT)
			{
				if(checkCommentData(_viewSpotsData.spotList)) return;
			}else if(_type == ClientConstants.DETAIL_HOUSE)
			{
				if(checkCommentData(_hotelData.hotelList)) return;
			}
			
			var mes:RequestComment = new RequestComment();
			mes.citySign = ClientConstants.CITY_ID;
			mes.requestId = itemId;
			mes.requestType = _type;
			mes.requestPage = page;
			
			this.send(mes);
		}
		
		public function commentThis(content:Object):void
		{
			var messageI:InsertComment = new InsertComment();
			messageI.evaluate = content.star;
			messageI.requestType = _type + 5;
			messageI.citySign = ClientConstants.CITY_ID;
			messageI.requestId = _itemID;
			messageI.userName = ClientConstants.USER_NAME;
			messageI.content = content.value;
			this.send(messageI);
		}
		
	}
}